<template>
  <div class="app-container clearfix">  
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="活动区域" prop="regionName">
          <el-input v-model="filterForm.regionName" clearable placeholder="活动区域" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="活动时间" prop="flashsaleTime">
          <el-date-picker 
          clearable
            v-model="filterForm.flashsaleTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="秒杀状态" prop="flashsaleStatus">          
            <el-radio-group v-model="filterForm.flashsaleStatus">
              <el-radio-button label="0">全部</el-radio-button>
              <el-radio-button label="1">未处理</el-radio-button>
              <el-radio-button label="2">已处理</el-radio-button>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
      <el-button @click="handleCreate()">新建</el-button>
    </div>
      <el-table
      :data="list"
      border
      style="width: 100%">
      <el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column
        label="标题">
        <template slot-scope="{row}">
          <div>{{ row.flashsaleName }}</div>
          <div>{{ row.flashsaleCode }} <i class="el-icon-copy-document"></i></div>
        </template>
      </el-table-column>
      <el-table-column
        label="适用区域" width="140">
        <template slot-scope="{row}">
          <div>{{ row.district }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="活动时间" width="200">
        <template slot-scope="{row}">
          <div>{{ row.beginTime }}</div>
          <div>{{ row.endTime }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.flashsaleStatus == 1" type="danger" effect="dark">待分配</el-tag>
          <el-tag v-else-if="row.flashsaleStatus == 2" type="warning" effect="dark">待处理</el-tag>
          <el-tag v-else-if="row.flashsaleStatus == 3" type="info" effect="dark">已作废</el-tag>
          <el-tag v-else-if="row.flashsaleStatus == 4" effect="dark">已添加</el-tag>
          <el-tag v-else effect="dark">未定义</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="最后修改"
        width="160">
        <template slot-scope="{row}">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="240">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row.ID)">编辑</el-button>
          <el-button type="danger" v-if="row.documentStatus == 5" @click="handleUpdate(row.ID)">删除</el-button>
          <el-button v-if="row.documentStatus == 3" type="info" @click="handleUpdate(row.ID)">下架</el-button>
          <el-button v-if="row.documentStatus == 4" type="primary" @click="handleUpdate(row.ID)">上架</el-button>
        </template>
      </el-table-column>
      </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getDeliveryList} from '@/api/sale/delivery'



export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      
      socket:"",
      tableKey: 0,
      list: null,
      windowHeight:"600",
      total: 0,
      listLoading: false,
      documentationList:[],
      connected:"loading",
      timer:null,
      filterForm: {
        page: 1,
        size: 10,
        flashsaleStatus:0,
        keyword: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList:function(){
      let _this = this;
      getDeliveryList(_this.filterForm).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleCreate:function(row){
      if(row == undefined){
        this.$router.push({ path: 'flashsale-add'})
      }else{
        this.$router.push({ path: 'flashsale-add', query: { 'parentDeliveryID': row.ID }})
      }
    },
    handleUpdate:function(flashsaleID){
        this.$router.push({ path: 'flashsale-add', query: { 'flashsaleID': flashsaleID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange:function(){

    },
    inintPrinter(callback){
      console.log("打印命令：："+callback)
      let _this = this;
      var serviceUrl = 'ws://'+this.cainiaoHost+':13528';

      socketPrint = new WebSocket(serviceUrl);

      // 监听消息
      socketPrint.onmessage = function(event)
      { 
        console.log('Client received a message', event);
        //defaultPrinter = JSON.parse(event.data).defaultPrinter
        // parse json
        var obj = eval('('+event.data+')');
        console.log(obj)
        switch(obj['cmd']){
          case 'getPrinters':
            _this.printerList = obj.printers;
            _this.printerDevice = obj.defaultPrinter
            break;
          case 'print':
		
            if (obj && (obj.previewURL || obj.previewImage))
            {
              var url;
              if (obj && obj.previewURL) {
                url = obj.previewURL;
              } else if (obj && obj.previewImage) {
                url = obj.previewImage;
              }
              var pdf = document.getElementById('pdf');
              if (pdf) {
                pdf.setAttribute('src', url);
                document.getElementById("pdfDiv").style.visibility="visible";
              }
            }
            _this.printData.list.forEach((v,key)=>{
              if(v['requestID'] == obj['requestID']){
                switch(obj['taskStatus']){
                  case 'rendered':
                    _this.printData.list[key]['status'] = '渲染完成'
                    break;
                  case 'failed':
                    _this.printData.list[key]['status'] = '打印失败,消息：'+obj['msg'];
                    _this.printData.printFailTotal = _this.printData.printFailTotal+1;
                    break;
                  case 'printed':
                    _this.printData.list[key]['status'] = '出纸完成'
                    break;
                }
              }
            })
            break;
          case 'notifyPrintResult':
            _this.printData.list.forEach((v,key)=>{
              if(v['requestID'] == obj['requestID']){
                switch(obj['taskStatus']){
                  case 'rendered':
                    _this.printData.list[key]['status'] = '渲染完成'
                    break;
                  case 'failed':
                    _this.printData.list[key]['status'] = '打印失败,消息：'+obj['msg'];
                    _this.printData.printFailTotal = _this.printData.printFailTotal+1;
                    break;
                  case 'printed':
                    _this.printData.list[key]['status'] = '出纸完成'
                    break;
                }
              }
            })

            break;
        }
      };

      socketPrint.onopen = function(event)
      {
        if (callback != null) {
          callback();
        }
        //链接成功，获取当前可用打印机
        var request = getRequestObject("getPrinters");
        console.log(request)
        socketPrint.send(JSON.stringify(request));
        setStatusLabel("已连接");
      }

      socketPrint.onerror = function(error)
      {
        console.log("Failed to connect CN print at " + serviceUrl, error);
        setStatusLabel("连接错误");
      }

      // 监听Socket的关闭
      socketPrint.onclose = function(event)
      { 
        console.log('Client notified socket has closed',event);
        setStatusLabel("连接关闭");
      }; 
    },
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>